<?php

class Ext_Attachment_Image_Add
{
    protected $_articleId;
    
    const IMAGE_EXISTS = 'imageExists';
    protected $_messageTemplates = array(
        self::IMAGE_EXISTS => "Image '%name%' already exists.",
    );
    protected $_messager;
    
    public function __construct($articleId)
    {
        $this->_articleId = $articleId;
        $this->_messager  = new Ext_Messager($this->_messageTemplates);
    }

    public function add($data)
    {
        $result = array(
            'code'     => Ext_Attachment_Image_Add_Result::FAILURE,
            'data'     => '',
            'messages' => array(),
        );
        
        $config = Ext_Config::getInstance()->getConfig('image');
        
        $imageIdArray = array();
        $articleImage = App_Model_DbTable_Article_Attachment_Image::getInstance()->getRowByArticleId($this->_articleId);
        if($articleImage){
            $ids = $articleImage['ids'];

            $idsArray = explode(';', $ids);
            for($i=0;$i<count($idsArray);$i++)
            {
                $attachmentImageId = $idsArray[$i];
                if(!$attachmentImageId){
                    continue;
                }

                $attachmentImage = App_Model_DbTable_Attachment_Image::getInstance()->getRowById($attachmentImageId);
                if(!$attachmentImage){
                    continue;
                }
                $imageIdArray[] = $attachmentImage['imageId'];
            }
        }
        
        $imageId = $data->imageId;
        if(in_array($imageId, $imageIdArray)){
            $result['messages'][] = $this->_messager->addMessage(self::IMAGE_EXISTS, array('name'=>$data->title));
            return new Ext_Attachment_Image_Add_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $row = array(
            'articleId' => $this->_articleId,
            'imageId'   => $imageId,
            'title'     => $data->title,
        );
        $attachmentImageId = App_Model_DbTable_Attachment_Image::getInstance()->addNew($row);
        $imager = new Ext_Article_Attachment_Image($this->_articleId);
        $imager->add($attachmentImageId);
        
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article){
            throw new Ext_Publish_Attachment_Add_Exception("Article record not found.");
        }
        
        $attachmentsArray = unserialize($article['attachments']);
        if(!is_array($attachmentsArray)){
            $attachmentsArray = array();
        }
        $attachment = array("type"=>'image', "id"=>$attachmentImageId);
        array_unshift($attachmentsArray, $attachment);
        if($attachmentsArray){
            $row = array(
                'attachments'  => serialize($attachmentsArray),
            );
            App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        }
        
        $result['code'] = Ext_Attachment_Image_Add_Result::SUCCESS;
        $result['data'] = $attachmentImageId;
        return new Ext_Attachment_Image_Add_Result($result['code'], $result['data'], $result['messages']);

    }
}
